package com.community.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.entity.Community;
import com.community.service.CommunityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * (Community)表控制层
 *
 * @author xn
 * @since 2021-09-27 16:45:30
 */
@Controller
@RequestMapping("community")
//说明接口文件
//@Api(value = "测试接口", tags = "用户管理相关的接口", description = "用户测试接口")
public class CommunityController extends ApiController {

    Page<Community> page = new Page<>();

    /**
     * 服务对象
     */
    @Resource
    private CommunityService communityService;

    @RequestMapping("communitylist")
    public String tolist() {
        return "communitylist";
    }


    @RequestMapping("communityadd")
    public String toadd() {
        return "communityadd";
    }

    /**
     * 分页查询所有数据
     *
     * @param current   当前页数
     * @param community 查询实体
     * @return 所有数据
     */
    @GetMapping("list")
    @ResponseBody
/*    @ApiOperation(value = "显示全部小区信息", httpMethod = "GET",
            protocols = "HTTP", produces = "application/json", notes = "小区分页")
    @ApiImplicitParam(name = "current", value = "当前页数")*/
//    @Cacheable(value = "community", keyGenerator = "keyGenerator")
    public R selectAll(Integer current, Community community) {
        page.setSize(2);
        page.setCurrent(current);
        QueryWrapper<Community> wrapper = new QueryWrapper<>();
        wrapper.like(StringUtils.isNotEmpty(community.getCommunityName()), "community_name", community.getCommunityName());
        wrapper.between(community.getBeginDate() != null && community.getOverDate() != null,
                "create_date", community.getBeginDate(), community.getOverDate());
        wrapper.eq(community.getState() != null, "state", community.getState());
        return success(this.communityService.page(page, wrapper));
    }

    /**
     * 返回下拉框数据
     *
     * @param community 查询实体
     * @return 所有数据
     */
    @GetMapping("getlist")
    @ResponseBody
/*    @ApiImplicitParam(name = "state", value = "小区状态")
    //说明是什么方法(可以理解为方法注释)
    @ApiOperation(value = "返回下拉框数据", notes = "所有小区数据")*/
    public R selectAll(Community community) {
        QueryWrapper<Community> wrapper = new QueryWrapper<>();
        wrapper.eq(community.getState() != null, "state", community.getState());
        return success(this.communityService.list(wrapper));
    }

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("{id}")
    @ResponseBody
    public R selectOne(@PathVariable Serializable id) {
        return success(this.communityService.getById(id));
    }

    /**
     * 修改数据
     *
     * @param community 实体对象
     * @return 修改结果
     */
    @PutMapping("update")
    @ResponseBody
//    @CachePut(value = "community", key = "#community.id")
    public R update(Community community) {
        community.setModifyDate(new Date());
        return success(this.communityService.updateById(community));
    }

    /**
     * 删除数据
     *
     * @param idList 主键结合
     * @return 删除结果
     */
    @DeleteMapping("delete")
    @ResponseBody
    public R delete(@RequestParam("idList") List<Long> idList) {
        return success(this.communityService.removeByIds(idList));
    }

    /**
     * 新增数据
     *
     * @param community 实体对象
     * @return 新增结果
     */
    @PostMapping("add")
    @ResponseBody
    public R insert(Community community) {
        community.setCreateDate(new Date());
        return success(this.communityService.save(community));
    }
}

